package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalParentResponse;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalTeacherResponse;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalParentResponse.LeaveApprovalParentListDetail;
import com.ycsj.goldmedalnewconcept.bean.LeaveApprovalTeacherResponse.LeaveApprovalListDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//家长请假列表
public class LeaveParentListActivity extends Activity implements
		OnClickListener {

	private ImageView iv_back;
	private ListView lv_leaveparent;
	private List<LeaveApprovalParentListDetail> list = new ArrayList<LeaveApprovalParentListDetail>();
	private MyAdapter mAdapter;
	private ImageView iv_newleave;
	private TextView tv_empty;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leaveparent);
		initView();
		setOnListener();
		
		//注册广播
		initBroadCast();
		
		initData();
	}

	
	
	/**
	 * 注册广播接受者当添加了广播后刷新界面
	 */
	
	private void initBroadCast() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(LeaveActivity.ACTION_LEAVE);
		registerReceiver(new MyReceiver(), filter);
	}
	
	/**
	 * 广播接受者者
	 * @author Administrator
	 *
	 */
	class MyReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			initData();
		}
	}
	

//	@Override
//	protected void onResume() {
//		initData();
//		
//		super.onResume();
//	}
//	
	
	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_newleave = (ImageView) findViewById(R.id.iv_newleave);
		lv_leaveparent = (ListView) findViewById(R.id.lv_leaveparent);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
	}

	
	private void initData() {
		String role = SPUtil
				.getString(LeaveParentListActivity.this, "role", "");

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
		final Request request = new Request.Builder()
				.url(Constant.SERVER_URL+"ManageMsg?type=15&c="+SPUtil.getString(this, "account", "")+"&role="+SPUtil.getString(this, "role", ""))
				.build();
		// new call13355556665
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);

	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		iv_newleave.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.iv_newleave:
			Intent intent = new Intent(LeaveParentListActivity.this,
					LeaveActivity.class);
			intent.putExtra("approval", approval);
			intent.putExtra("noapproval", noapproval);
			startActivity(intent);
			break;
		}
	}

	class ViewHolder {
		private TextView tv_approval;
		private TextView tv_time;
		private TextView tv_content;
	}
	int approval=0;
	int noapproval=0;
	class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater = null;

		private MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater
						.inflate(R.layout.list_leaveparent, null);
				holder.tv_approval = (TextView) convertView
						.findViewById(R.id.tv_approval);
				holder.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				holder.tv_content = (TextView) convertView
						.findViewById(R.id.tv_content);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			
			if ("1".equals(list.get(position).STATUS)) {
				approval++;
				holder.tv_approval.setText("已批准");
				holder.tv_approval.setBackgroundResource(R.drawable.shape_gary_bg);
			} else if ("0".equals(list.get(position).STATUS)) {
				noapproval++;
				holder.tv_approval.setText("待批准");
				holder.tv_approval.setBackgroundResource(R.drawable.shape_orange_5);
			}
			holder.tv_time.setText("请假时间：" + list.get(position).BEGIN_DATE
					+ "~" + list.get(position).END_DATE);
			holder.tv_content.setText("请假内容："+list.get(position).CONTENT);
			Log.e("test", "approval="+approval);
			return convertView;
		}
	}

	Handler handler = new Handler();
	Callback callBack = new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("test", json);
			Gson gson = new Gson();
			list.removeAll(list);
			if ("{\"state\":\"-1\"}".equals(json)) {
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						tv_empty.setVisibility(tv_empty.VISIBLE);
					}
				}, 0);

				return;
			}
			if (!("{\"state\":\"-1\"}").equals(json)) {
				LeaveApprovalParentResponse fromJson = gson.fromJson(json,
						LeaveApprovalParentResponse.class);

				list=fromJson.list;

				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						tv_empty.setVisibility(tv_empty.GONE);
						noapproval = 0;
						mAdapter = new MyAdapter(LeaveParentListActivity.this);
						lv_leaveparent.setAdapter(mAdapter);
					}
				}, 0);
			}
		}

	};

	Callback callback2 = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			// TODO Auto-generated method stub
			String json = arg0.body().string();
			Log.e("test", json);
			if ("{\"state\":\"-1\"}".equals(json)) {
				tv_empty.setVisibility(tv_empty.VISIBLE);
			}
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};

}
